package com.dces.jx.billtest.api.userAdd;
import com.dces.jx.billtest.dto.userAdd.*;
import com.dces.jx.billtest.common.service.ExcelService;
import com.dces.jx.billtest.common.model.PCodeType;
import com.dces.jx.billtest.dto.userAdd.DcBillListZhjDto;
import com.dces.jx.billtest.userAdd.service.DcBillListZhjService;
import com.google.inject.internal.cglib.core.$ReflectUtils;
import com.xdo.pcode.service.PCodeHolder;
import com.xdo.common.token.UserInfoToken;
import com.xdo.domain.PageParam;
import com.xdo.domain.ResultObject;
import com.xdo.springboot.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Digits;
import javax.xml.transform.Result;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * generated by Generate dc
 * 
 *
 * @author xdoV2.0
 * @date: 2021-9-3
 */
@RestController
@RequestMapping("v1/dcBillListZhj")
@Api(tags = "接口")
public class DcBillListZhjController extends BaseController {
    @Resource
    private DcBillListZhjService dcBillListZhjService;
    @Resource
    private PCodeHolder pCodeHolder;
    @Resource
    private ExcelService excelService;
    /**
     * @param dcBillListZhjParam
     * @param pageParam
     * @return
     */
    @ApiOperation("分页查询接口")
    @PostMapping("list")
    public ResultObject<List<DcBillListZhjDto>> getListPaged(@RequestBody DcBillListZhjParam dcBillListZhjParam, PageParam pageParam) {
        ResultObject<List<DcBillListZhjDto>> paged = dcBillListZhjService.selectAllPaged(dcBillListZhjParam, pageParam);
        return paged;
    }
    /**
     * @param dcBillListZhjParam
     * @param userInfo
     * @return
     */
    @ApiOperation("新增接口")
    @PostMapping()
    public ResultObject<DcBillListZhjDto> insert(@Valid @RequestBody DcBillListZhjParam dcBillListZhjParam, UserInfoToken userInfo) {
		ResultObject<DcBillListZhjDto> resultObject = ResultObject.createInstance(true, ResultObject.INSERT_SUCCESS);
        // 插入前的校验
        String message = dcBillListZhjService.beforeSaveAndInsert(dcBillListZhjParam);
        if(!"".equals(message)){
            resultObject.setSuccess(false);
            resultObject.setMessage(message);
            return resultObject;
        }
        // 设置参数
        DcBillListZhjParam param = dcBillListZhjService.setParam(dcBillListZhjParam);
        DcBillListZhjDto dcBillListZhjDto = dcBillListZhjService.insert(param, userInfo);
        if (dcBillListZhjDto != null) {
            resultObject.setData(dcBillListZhjDto);
        } else {
            resultObject.setSuccess(false);
            resultObject.setMessage(ResultObject.INSERT_FAIL);
        }
        return resultObject;
    }
    /**
     * @param sid
     * @param dcBillListZhjParam
     * @param userInfo
     * @return
     */
    @ApiOperation("修改接口")
    @PutMapping("{sid}")
    public ResultObject<DcBillListZhjDto> update(@PathVariable String sid, @Valid @RequestBody DcBillListZhjParam dcBillListZhjParam, UserInfoToken userInfo) {
        dcBillListZhjParam.setSid(sid);
        // 插入前的校验
        ResultObject<DcBillListZhjDto> resultObject = ResultObject.createInstance(true, ResultObject.UPDATE_SUCCESS);

        String message = dcBillListZhjService.beforeSaveAndInsert(dcBillListZhjParam);
        if(!"".equals(message)){
            resultObject.setSuccess(false);
            resultObject.setMessage(message);
            return resultObject;
        }
        DcBillListZhjDto dcBillListZhjDto = dcBillListZhjService.update(dcBillListZhjParam, userInfo);
        if (dcBillListZhjDto != null) {
            resultObject.setData(dcBillListZhjDto);
        } else {
            resultObject.setSuccess(false);
            resultObject.setMessage(ResultObject.UPDATE_FAIL);
        }
        return resultObject;
    }
    /**
     * @param sids
     * @return
     */
    @ApiOperation("删除接口")
    @DeleteMapping("{sids}")
    public ResultObject delete(@PathVariable List<String> sids, UserInfoToken userInfo) {
		ResultObject resultObject = ResultObject.createInstance(true, ResultObject.DELETE_SUCCESS);
        dcBillListZhjService.delete(sids, userInfo);
        return resultObject;
    }
    @ApiOperation("获取序列号")
    @PostMapping("/serialNo")
    public ResultObject getSerialNo(@RequestParam String headId){
        long serialNo = dcBillListZhjService.getSerialNo(headId);
        return ResultObject.createInstance(true,"查询成功",serialNo);
    }
    @ApiOperation("根据料号查询")
    @PostMapping("/queryCopGNo")
    public ResultObject queryCopGNo(@RequestBody CopGNoAndEmsNoParam gNoAndEmsNoParam){
        return dcBillListZhjService.queryCopGNO(gNoAndEmsNoParam);
    }
    @ApiOperation("根据备案查询")
    @PostMapping("/queryGNo")
    public ResultObject queryGNo(@RequestBody GNoAndEmsNoParam gNoAndEmsNoParam){
        // 去数据库查询是否存在
        return dcBillListZhjService.queryGNo(gNoAndEmsNoParam);
    }
    @ApiOperation("校验商品料号与备案序号")
    @PostMapping("/checkGNoAndCopGNo")
    public ResultObject checkGNoAndCopGNo(@RequestBody CheckGNoAndCopNoParam param){
        int index = dcBillListZhjService.checkGNoAndCopGNo(param);
        if(index != 0){
            return ResultObject.createInstance(true);
        }
        return ResultObject.createInstance(false,"商品料号与备案序号不一致!");

    }

    @ApiOperation("/清空所有")
    @DeleteMapping ("/deleteAll/{headIds}")
    public ResultObject deleteAll(@PathVariable("headIds") List<String> headIds){
        dcBillListZhjService.deleteByHeadIds(headIds);
        return ResultObject.createInstance(true,"清空成功");
    }

    @ApiOperation("/查询汇总数量")
    @GetMapping ("/getNumSum")
    public ResultObject getNumSum(@RequestParam("headId") String headId){
        Map<String,BigDecimal> info =dcBillListZhjService.getNumSum(headId);
        return ResultObject.createInstance(true,"查询成功",info);
    }
}
